import numpy as np
import cv2
import glob
import matplotlib.pyplot as plt

from calibrate import calibrate

import log


def capture_event(event, x, y, flags, params):
    if event == cv2.EVENT_LBUTTONDOWN or event == cv2.EVENT_RBUTTONDOWN:
        print("\033[1;35m"+f'{x}, {y}'+"\033[0m")

def readBins(path='bins/*.bin'):
    images = glob.glob(path)
    for idx, fname in enumerate(images):
        fname = 'bins/33.bin'
        
        # print("\033[1;32m"+f'{fname}'+"\033[0m", end=':   ')
        print("\033[1;32m"+f'{fname}'+"\033[0m :")

        f = open(fname, 'rb')
        data = f.read()
        img = np.frombuffer(data, dtype=np.uint8)
        gray = np.reshape(img, (240, 320))
        origin = cv2.cvtColor(gray,cv2.COLOR_GRAY2BGR)
        # cv2.imwrite('bins/current.jpg', origin)

        ####################################################
        calibrate(gray)
        ####################################################
        cv2.imshow('im_show', origin)
        cv2.setMouseCallback('im_show', capture_event)

        cv2.waitKey(0)
        
cv2.destroyAllWindows()       
        
if __name__ == '__main__':
    log.setupLog(False)
    readBins()